from io import BytesIO
from application.util import UpyunUtil, ImageBedUtil
from application.config.ImageUploadConfig import ImageBedConfig, UpyunConfig


def upload_image(files: list, file_bytes: BytesIO) -> dict:
    """
    上传图片
    :param files: 文件对象列表：[("file",(image_storage.filename, image_io, "image/*"))]
    :param file_bytes: 文件字节流
    :return: {
        "unique_id": image_id,
        "url": image_url
    }
    """
    # 如果启用了AList服务
    if UpyunConfig.use:
        response: dict = UpyunUtil.upload_image(filename=files[0][1][0], file_bytes=file_bytes)
        return {
            "unique_id": response.get("id"),
            "url": response.get("url")
        }
    elif ImageBedConfig.use:
        response: dict = ImageBedUtil.upload_image(files=files)
        return {
            "unique_id": response.get("id"),
            "url": response.get("url")
        }


def delete_image(unique_id: str) -> None:
    """
    删除图片
    :param unique_id: 图片唯一标识
    :return
    """
    # 如果启用了AList服务
    if UpyunConfig.use:
        UpyunUtil.delete_image(filename=unique_id)
    elif ImageBedConfig.use:
        ImageBedUtil.delete_image(unique_id=unique_id)
